import cfg from '../../config';
import {getView} from '../../util/api';
import {formatDate, backPage, tabSwitch, message} from "../../util/aid";
import {getSeatParam, setHeadTrianInfo, loadJingTinInfoFun, orderGaiQianFun, queryDateFun, starDateValFun, cityViewFun, bindCityFun, findOrderDelData, refundsOrder, changeSignOrder, getTicketnosStr, concelOrder} from '../common';
import {tripsObj, orderStatus, orderCls, perKey, planKey, changeSignStatus, changeSignCls} from "../const.config";

const $ = require('../../lib/kdom');
let _view = require('raw-loader!./order-detail.html');

let _opts = null;
let _orderData = null;
let ticketnos = [];
let outOrderID = '';

export default class {
    path = '/train/order-detail$';
    hash = '/train/order-detail';
    title = '订单详情';

    constructor(opt) {
        opt = opt || {};
        this.path = opt.path || this.path;
        this.hash = opt.hash || this.hash;
        this.title = opt.title || this.title;
    }

    // 输出视图
    view(cb) {
        if (!_view) {
            // 静态资源浏览器有缓存,增加时标,强制刷新!
            getView(`${cfg.train.view.orderDetail}?t=${(+new Date())}`, '', (rs) => {
                _view = rs;
                cb(null, _view);
            });
        } else
            cb(null, _view);
    }

    // 在已经加载的视图上操作
    bind(dv, params) {
        // console.log('我是订单详情：', params);
        _orderData = params.orderData;
        ticketnos = [];
        outOrderID = '';

        // orderDelNavFun
        orderDelNavFun(_orderData);

        // orderDelViewFun
        orderDelViewFun(_orderData);

        // backPage
        backPage('#pg-train-order-detail', 'backPage', () => {
            $.router.go('/train/order', '', true);
        });
    }

    /*bind*/
}

function orderDelNavFun(obj) {
    // console.log(obj);
    if (!obj) return;
    obj = obj.orderInfo;
    let _status = obj.status;
    let rc = '';
    let concel = '';
    let orderPayMent = '';
    let changeSign = '';
    let refunds = '';
    outOrderID = obj.outOrderID;
    let Tickets = '';

    let orderRCAll = obj.orderRCAll;
    Tickets = orderRCAll ? JSON.parse(orderRCAll) : '';
    Tickets = Tickets.data ? Tickets.data.Tickets : '';
    if (new Date(obj.sdate) > new Date()) {
        if (_status == 1 || _status == 2) {
            concel = `<div class="tab-item" id="orderQuXiao" data-val='${outOrderID}'>取消</div>`;
            if (_status == 2) {
                orderPayMent = `<div class="tab-item text-danger" id="orderPayMent">支付</div>`;
            }
        }

        if (_status == 7) {
            changeSign = `<div class="tab-item text-primary" id="orderTuiPiao">退票</div>`;
        }

        if (_status == 7) {
            if (Tickets) {
                ticketnos = getTicketnosStr(Tickets);
            }
            // console.log('ticketnos', ticketnos);
            refunds = `<div class="tab-item text-danger" id="orderGaiQian" data-val='${outOrderID},${ticketnos.join(',')}'>改签</div>`;
        }
    }
    rc = concel + changeSign + refunds + orderPayMent;
    $.html(orderDelNavBox, rc);
    bindEvOrderDel();
}

function bindEvOrderDel() {
    let orderInfo = _orderData.orderInfo;
    let rs = orderInfo.orderRCAll;
    rs = JSON.parse(rs).data;
    // 取消订单
    if ($.id('orderQuXiao')) {
        orderQuXiao.onclick = () => {
            concelOrder(outOrderID);
        };
    }
    // orderPayMent
    if ($.id('orderPayMent')) {
        orderPayMent.onclick = () => {
            payorderFun(rs);
        };
    }

    // 退票
    if ($.id('orderTuiPiao')) {
        orderTuiPiao.onclick = () => {
            refundsOrder(outOrderID, ticketnos);
        };
    }

    // 改签
    if ($.id('orderGaiQian')) {
        orderGaiQian.onclick = () => {
            changeSignOrder(orderInfo);
        };
    }
}

function orderDelViewFun(data) {
    // console.log(data);

    let obj = data.orderInfo;
    let order = JSON.parse(obj.orderRCAll);
    let temp = '';
    let curST = '';
    let _status = obj.status;
    if (order.successcode === "T") {
        let _ageType = {1: '成人票', 2: '儿童票'};
        let _cardType = {1: '二代身份证', 2: '护照', 5: '港澳通行证', 7: '台湾居民来往大陆通行证'};
        for (let i = 0, o = order.data.Tickets; i < o.length; i++) {
            temp += `<div class="card-content-cell">
              <span>${o[i].PassengerName}</span>
              <span>${_cardType[o[i].passengerIdCardType]}</span>
              <span>${_ageType[o[i].TicketType]}</span>
            </div>`;
        }
    }
    if (new Date(obj.sdate) - new Date() <= 20 * 60 * 1000) {
        curST = `<span class="${orderCls[obj.status]}">${orderStatus[obj.status]}</span>`;
    } else {
        if (_status == 1 || _status == 2) {
            curST = `<span class="${orderCls['9']}">${orderStatus['9']}</span>`;
        } else {
            curST = `<span class="${orderCls[obj.status]}">${orderStatus[obj.status]}</span>`;
        }
    }
    let rc = `<div class="content-block-title">单号：${obj.outOrderID || '无'}</div>
    <div class="list-block cards-list">
        <ul>
            <li class="card">
                <div class="card-header">
                    <span>${obj.fromstation} - ${obj.tostation}</span>
                   ${curST}
                </div>
                <div class="card-content">
                    <div class="card-content-inner">
                        <div class="card-content-cell">
                            <span>${obj.traincode} ${tripsObj[obj.traincode.substring(0, 1)]}</span>
                            <span>${obj.seatname}</span>
                            <b class="text-danger"><span class="f06">￥</span>${obj.totalprice}</b>
                        </div>
                       ${temp}
                    </div>
                </div>
                <div class="card-footer">
                    <span></span>
                    <span>${formatDate('yyyy-MM-dd hh:mm', obj.sdate)}</span>
                </div>
            </li>
        </ul>
    </div>`;
    $.html(orderListDelBox, rc);
}